package com.example.demothree.controller;

import com.example.demothree.bean.*;
import com.example.demothree.dao.SaleOrderDao;
import com.example.demothree.service.SaleService;
import com.example.demothree.service.SkService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("sale")
public class SaleController {
    @Resource
    private SaleService saleService;

    @Resource
    private SkService skService;
    private SaleOrder saleOrder;
    private SaleReturnOrder saleReturnOrder;
    private Long saleId = 0L;
    private Stock stock;
    @RequestMapping("salexsdd")
    public String salexsdd(Model model,
                           @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.saleId != 0L){//判断是否删除成功
            Long deleteXsdd1 = this.saleId;
            model.addAttribute("deleteXsdd1",deleteXsdd1);
        }
        this.saleId = 0L;
        //分页
        Page<SaleOrder> pagexsdd = saleService.saleXsdd(page,size);
        model.addAttribute("pagexsdd",pagexsdd);
        return "salexsdd";
    }
    //添加销售订单
    @RequestMapping("addxsdd")
    public String addxsdd(Model model) {
        if (this.saleId.equals(1L)) {
            model.addAttribute("saleOrder",saleOrder);
        }else {
            int a = 1;
            System.out.println(a);
            model.addAttribute("a", a);
        }
        this.saleId = 0L;
        return "addxsdd";
    }
    @GetMapping("addxsddcz/{id}")
    public String addxsddcz(@PathVariable Long id) {
        if (id != null) {
            saleOrder = saleService.findById(Long.valueOf(id));
            this.saleId = 1L;
        }
        return "forward:/sale/addxsdd";
    }

    @RequestMapping("addxsddzc")
    public String addxsddzc(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        String goodsId = request.getParameter("goodsId");
        String customerId = request.getParameter("customerId");
        String pay = request.getParameter("pay");
        String date = request.getParameter("date");
        String total = request.getParameter("total");
        String flag = request.getParameter("flag");
        String count = request.getParameter("count");
        int xsddadd = 0;
        try {
            if (id != null) {
                //修改
                saleOrder = saleService.findById(Long.valueOf(id));
            }else {
                //添加
                saleOrder = new SaleOrder();
            }
            saleOrder.setGoodsId(Long.valueOf(goodsId));
            saleOrder.setCustomerId(Long.valueOf(customerId));
            saleOrder.setPay(pay);
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date myDate1 = dateFormat.parse(date);
            saleOrder.setDate(myDate1);
            saleOrder.setTotal(Double.valueOf(total));
            saleOrder.setFlag(flag);
            saleOrder.setCount(Long.valueOf(count));
            saleService.addxsdd(saleOrder);
            xsddadd = 100;
        }catch (Exception e) {
            xsddadd = 200;
            e.printStackTrace();
        }
        response.getWriter().print(xsddadd);
        return null;
    }
    //销售订单退货
    @GetMapping("salexsddDelete/{id}")
    public String purcgddDelete(@PathVariable Long id) throws ParseException {
        try {
            //退货订单添加
            saleOrder = saleService.findById(id);
            Long goodsId = saleOrder.getGoodsId();
            Long cousomerId = saleOrder.getCustomerId();
            String pay = saleOrder.getPay();
            Double total = saleOrder.getTotal();
            Long count = saleOrder.getCount();
            saleReturnOrder = new SaleReturnOrder();
            saleReturnOrder.setGoodsId(goodsId);
            saleReturnOrder.setCustomerId(cousomerId);
            saleReturnOrder.setPay(pay);
            saleReturnOrder.setTotal(total);
            saleReturnOrder.setCount(count);
            //添加当前时间
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
            Date date1 = dateFormat.parse(dateFormat.format(date));
            saleReturnOrder.setDate(date1);

            stock = skService.findspidBy(saleOrder.getGoodsId());
            if (saleOrder.getFlag().equals("已出库")){
            if (stock != null) {
                stock.setCounts(stock.getCounts()+saleOrder.getCount());
            }else {
                stock = new Stock();
                stock.setArea("临时仓库");
                stock.setGoodsId(saleOrder.getGoodsId());
                stock.setCounts(saleOrder.getCount());
            }
            skService.addkc(stock);
            }
            saleService.addxsth(saleReturnOrder);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //销售订单按id退货
        saleService.deletexsdd(Long.valueOf(id));
        //退货弹窗
        this.saleId = id;
        return "forward:/sale/salexsdd";
    }
    //发货出库
    @RequestMapping("salefhck")
    public String salefhck(Model model,
                           @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.saleId != 0L){//判断是否出库成功
            Long deleteFhck1 = this.saleId;
            model.addAttribute("deleteFhck1",deleteFhck1);
        }
        this.saleId = 0L;
        //分页
        Page<SaleOrder> pagefhck = saleService.saleXsdd(page,size);
        model.addAttribute("pagefhck",pagefhck);
        return "salefhck";
    }
    //销售订单出库
    @GetMapping("salefhckCk/{id}")
    public String salefhckCk(@PathVariable Long id) {
        try {
            //出库操作
            saleOrder = saleService.findById(id);
            if (saleOrder.getFlag().equals("未出库")) {
                Long count = saleOrder.getCount();
                Long goodsId = saleOrder.getGoodsId();
                stock = skService.findspidBy(goodsId);
                Long counts = stock.getCounts();
                if (stock != null && counts > count) {
                    stock.setCounts(counts-count);
                    System.out.println(counts+"|||||||"+count);
                    skService.addkc(stock);
                    //入库弹窗
                    this.saleId = id;
                }else {
                    this.saleId = -3L;
                }
                System.out.println("|||||||");
                saleOrder.setFlag("已出库");
            }else  {
                //入库弹窗
                this.saleId = -1L;
            }


            saleService.addxsdd(saleOrder);
        } catch (Exception e) {
            e.printStackTrace();
            //入库弹窗
            this.saleId = -2L;
        }
        //入库弹窗
        return "forward:/sale/salefhck";
    }
    @RequestMapping("salexsth")
    public String salexsth(Model model,
                           @RequestParam(value = "page", defaultValue = "0") Integer page,
                           @RequestParam(value = "size", defaultValue = "5") Integer size) {
        if (this.saleId != 0L){//判断是否删除成功
            Long deleteXsth1 = this.saleId;
            model.addAttribute("deleteXsth1",deleteXsth1);
        }
        this.saleId = 0L;
        //分页
        Page<SaleReturnOrder> pagexsth = saleService.saleXsth(page,size);
        model.addAttribute("pagexsth",pagexsth);
        return "salexsth";
    }
    @GetMapping("deleteXsth/{id}")
    public String deleteXsth(@PathVariable Long id) {
        saleService.deletexsth(id);
        this.saleId = id;
        return "forward:/sale/salexsth";
    }
}
